package com.etrans.web.gps.business.orderReceiving.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.orderReceiving.domain.Alarm;
import com.etrans.web.gps.business.orderReceiving.service.OrderReceivingService;
import com.etrans.web.gps.business.system.domain.AlarmTypeVO;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/orderReceiving/OrderReceiving/")
public class OrderReceivingController extends BaseController{
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private OrderReceivingService orderReceivingService;
	
	
	@RequestMapping("toOrderReceiving.html")
	public String  toOrderReceiving(){
		return "views/business/orderReceiving/orderReceiving";
	}
	
	@RequestMapping("tosaveHandleDeatils.html")
	public String  tosaveHandleDeatils(){
		return "views/business/orderReceiving/option";
	}
	
	@RequestMapping("loadAlarmType.html")
	public @ResponseBody List<AlarmTypeVO> loadAlarmType(HttpServletRequest request) {
		String name = request.getParameter("name");
		HashMap<String, Object> param = new HashMap<>();
		SysUser user=getSysUser();
		param.put("FullID", String.format("%08d", user.getArea_id()));
		param.put("name", name);
		param.put("selected", "selected");
		return commonService.queryAlarmTypeList();
	}
	
	@RequestMapping("queryTable.html")
	public @ResponseBody PageVo queryTable(){
		/*SysUser user = getSysUser();
		Integer area_id = user.getArea_id();
		HashMap<Integer,Object> entMap = new HashMap<Integer, Object>();
		HashMap<Integer,Object> platformMap = new HashMap<Integer, Object>();
		List<HashMap<String, Object>> list = commonService.queryPlatByRole(area_id, null);	
		for (HashMap<String, Object> hashMap : list) {
			Integer entId = ((Long)hashMap.get("entId")).intValue();
			String entName = (String)hashMap.get("entName");
			entMap.put(entId, entName);
			Integer platformCode = (int)hashMap.get("platformCode");
			String platName = (String)hashMap.get("platName");
			platformMap.put(platformCode, platName);
		}*/
		PageVo vo = new PageVo();
		try {
		HashMap<String, Object> paramMap = this.getReqParameters();
		Map<String, Object> params = FlexiGridUtil.parseJSONParam(paramMap.get("paramsGrid").toString()).getWhereParam();
		//PageVo vo = orderReceivingService.queryTable(params,entMap,platformMap);
			vo = orderReceivingService.queryTable(params);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	@RequestMapping("saveHandleDetail.html")
	public @ResponseBody Map<String,Object> saveHandleDetail(){
		Map<String,Object> rmap = new HashMap<String,Object>();
		Map<String,Object> map = this.getReqParameters();
		rmap = orderReceivingService.saveHandleDetail(map);
		return rmap;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "export.html")
	public void export( HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid, String colModel, String title, String StartPage, String EndPage, String pagesize) {
		try {
			Map<String, Object> paramMap=getReqParameters();
			List<String> colModelList = JSON.parseObject(colModel, List.class);
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("page",Integer.parseInt(StartPage)-1);
			param.put("pageSize",(Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
			paramMap.putAll(param);
			PageVo pageVo = orderReceivingService.queryTable(paramMap);
			List<Alarm> list = (List<Alarm>)pageVo.getRows();
			List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
			int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
			int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
			if(end>(list.size()-1)){
				end=list.size();
			}
			for(int i=start;i<end;i++){
				HashMap<String, Object> map = new HashMap<String, Object>();
				Alarm obj=(Alarm)list.get(i);
				map.put("vehicle_no", obj.getVehicle_no()==null ? "" : obj.getVehicle_no());
				map.put("vehicle_color_name", obj.getVehicle_color_name()==null ? "" : obj.getVehicle_color_name());
				map.put("enterprise_name", obj.getEnterprise_name()==null ? "" : obj.getEnterprise_name());
				map.put("platform_name", obj.getPlatform_name()==null ? "" : obj.getPlatform_name());
				map.put("alarm_type_name", obj.getAlarm_type_name()==null ? "" : obj.getAlarm_type_name());
				map.put("alarm_src", obj.getAlarm_src()==null ? "" : obj.getAlarm_src()==1?"终端":"企业平台");
				map.put("alarm_time", obj.getAlarm_time()==null ? "" : DateUtil.getDateToString(obj.getAlarm_time(),"yyyy-MM-dd HH:mm:ss"));
				map.put("handle_type", obj.getHandle_type()==null? "未处理" :obj.getHandle_type()==1?"系统处理":"人工处理");
				map.put("handle_time", obj.getHandle_time()==null ? "" : obj.getHandle_time());
				map.put("handle_remark", obj.getHandle_remark()==null ? "" : obj.getHandle_remark());
				rows.add(map);
			}
			int index=-1;
			List rowsList = new ArrayList<>();
			System.out.println(rows.size()+"-----------------------------------------------------------------");
			rowsList.add(rows);
			exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("queryalarmhandle.html")
	public @ResponseBody Map<String,Object> queryalarmhandle(){
		Map<String,Object> map = this.getReqParameters();
		return orderReceivingService.queryalarmhandle(map);
	}
}
